#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>


int main(int argc, char *argv[])
{
    if(argc != 2) exit(1);
    //close(1)
    //int fd = open("log.txt",O_CREAT|O_WRONLY|O_TRUNC,0666);
    //int fd = open("log.txt",O_CREAT|O_WRONLY|O_APPEND,0666);
    int fd = open(argv[1],O_RDONLY,0666);
    if(fd < 0) exit(1);

    dup2(fd, 0);
    close(fd);

    while(1)
    {
        char buffer[64];
        if(!fgets(buffer,sizeof(buffer),stdin))break;
        printf("%s",buffer);
    }



    //printf("fd: %d\n", fd);
    // printf("hello world\n");
    // printf("hello world\n");
    // printf("hello world\n");
    // fprintf(stdout,"hello hello\n");
    // fprintf(stdout,"hello hello\n");
    // fprintf(stdout,"hello hello\n");
    // fprintf(stdout,"hello hello\n");
    
    //close(1);//close(fd)//使用这行代码，会出现一个奇怪现象？？？
    return 0;
}


//int main()
//{
//    printf("stdin: %d\n",stdin->_fileno);
//    printf("stdout: %d\n",stdout->_fileno);
//    printf("stderr: %d\n",stderr->_fileno);
//
//
//
//    umask(0);
//    int fd1 = open("log1.txt",O_CREAT|O_WRONLY|O_TRUNC,0666);
//    int fd2 = open("log2.txt",O_CREAT|O_WRONLY|O_TRUNC,0666);
//    int fd3 = open("log3.txt",O_CREAT|O_WRONLY|O_TRUNC,0666);
//    int fd4 = open("log4.txt",O_CREAT|O_WRONLY|O_TRUNC,0666);
//    if(fd1 < 0)exit(1);
//    if(fd2 < 0)exit(1);
//    if(fd3 < 0)exit(1);
//    if(fd4 < 0)exit(1);
//    printf("fd1: %d\n", fd1);
//    printf("fd2: %d\n", fd2);
//    printf("fd3: %d\n", fd3);
//    printf("fd4: %d\n", fd4);
//
//
//    close(fd1);
//    close(fd2);
//    close(fd3);
//    close(fd4);
//    return 0;
//}
//
//
//
//
//// int main()
//// {
////     umask(0);
////     //int fd = open("log.txt",O_CREAT|O_WRONLY|O_TRUNC,0666);
////     //int fd = open("log.txt",O_CREAT|O_WRONLY|O_APPEND,0666);
////     int fd = open("log.txt",O_RDONLY);
//
////     if(fd < 0)
////     {
////         perror("open");
////         return 1;
////     }
//
//
//
////     while(1)
////     {
////         char buffer[64];
////         int n = read(fd,buffer,sizeof(buffer)-1);//-1是为了给字符串至少留一个停止标志
////         if(n > 0)
////         {
////             buffer[n] = 0;
////             printf("%s",buffer);
////         }
////         else if(n == 0)//是不是和我们的feof有点......嘻嘻
////         {
////             break;
////         }
////     }
//
////     //printf("fd: %d\n",fd);
////     // const char * msg = "abcd";
////     // int cnt = 1;
////     // int a = 1234567;
//
////     // while(cnt--)
////     // {
////     //     //当作字符串来写--->
////     //     char buffer[16];
////     //     snprintf(buffer,sizeof(buffer),"%d",a);//将其格式化成字符串
////     //     write(fd,buffer,strlen(buffer));
////     // }
//    
////     close(fd);
////     return 0;
//// }
